package com.szq.lottery.application.mq;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

import javax.annotation.Resource;

/**
 * @author 吾空白
 * @description: 消息生产者
 */
@Component
public class KafkaProducer2 {
    private Logger logger= LoggerFactory.getLogger(KafkaProducer2.class);

    @Resource
    private KafkaTemplate<String,Object> kafkaTemplate;

    public static final String TOPIC_TEST="Hello-Kafka";

    public static final String TOPIC_GROUP="test-consumer-group";

    public void send(Object object){
        String obj2String= JSON.toJSONString(object);
        logger.info("准备发送消息为:{}",obj2String);

        //发送消息
        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(TOPIC_TEST, object);
        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            public void onFailure(Throwable throwable) {
                //发送失败的处理
                logger.info(TOPIC_TEST+"-生产者 发送消息失败: "+throwable.getMessage());
            }

            @Override
            public void onSuccess(SendResult<String, Object> stringObjectSendResult) {
                //成功的处理
                logger.info(TOPIC_TEST+"-生产者 发送消息成功："+stringObjectSendResult.toString());
            }
        });
    }
}
